﻿using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Zhao.Common.Helper
{
    /// <summary>
    /// 对象转换json
    /// </summary>
    public static class JsonExtend
    {
        public static object ToJson(this string Json, bool isLower = true)
        {
            var setting = new JsonSerializerSettings()
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                DateFormatString = "yyyy-MM-dd HH:mm:ss",
            };
            if (isLower)
            {
                setting.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
            }
            return Json == null ? null : JsonConvert.DeserializeObject(Json, setting);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="isLower">是否首字母小驼峰</param>
        /// <returns></returns>
        public static string ToJson(this object obj, bool isLower = true)
        {
            var setting = new JsonSerializerSettings()
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                DateFormatString = "yyyy-MM-dd HH:mm:ss",
            };
            if (isLower)
            {
                setting.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
            }
            return JsonConvert.SerializeObject(obj, setting);
        }
        public static string ToJson(this object obj, string datetimeformats, bool isLower = true)
        {
            var setting = new JsonSerializerSettings()
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                DateFormatString = "yyyy-MM-dd HH:mm:ss",
            };
            if (isLower)
            {
                setting.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
            }
            return JsonConvert.SerializeObject(obj, setting);
        }
        public static T ToObject<T>(this string Json)
        {
            try
            {
                var setting = new JsonSerializerSettings
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                    ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(),
                };
                return Json == null ? default(T) : JsonConvert.DeserializeObject<T>(Json, setting);
            }
            catch
            {
                return default(T);
            }
        }
        public static List<T> ToList<T>(this string Json, bool isLower = true)
        {
            try
            {
                var setting = new JsonSerializerSettings
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                    ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(),
                };
                return Json == null ? null : JsonConvert.DeserializeObject<List<T>>(Json, setting);
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 转换成通用Json对象
        /// </summary>
        /// <param name="Json"></param>
        /// <returns></returns>
        public static JObject ToJObject(this string Json)
        {
            try
            {
                return JObject.Parse(Json);
            }
            catch
            {
                return null;
            }
        }
        public static DataTable ToTable(this string Json)
        {
            try
            {
                return Json == null ? null : JsonConvert.DeserializeObject<DataTable>(Json);
            }
            catch
            {
                return null;
            }
        }
    }
}
